//
//  XFXNewsHomeViewController.m
//  XFXnews
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXNewsHomeViewController.h"
#import "XFXNewsViewController.h"
#import "XFXRecreationViewController.h"
#import "XFXSportViewController.h"
#import "XFXScienceViewController.h"
#import "XFXFashionViewController.h"

@interface XFXNewsHomeViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *bigScrollView;
// 跟随按钮一起滑动的小红条
@property (nonatomic, retain) UIView *sliderView;
// 头条按钮
@property (nonatomic, retain) UIButton *headLine;
// 娱乐按钮
@property (nonatomic, retain) UIButton *recreation;
// 体育按钮
@property (nonatomic, retain) UIButton *sport;
// 科技按钮
@property (nonatomic, retain) UIButton *science;
// 时尚按钮
@property (nonatomic, retain) UIButton *fashion;

@end

@implementation XFXNewsHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    //外层scrollview
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    self.bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40 * XFXSHEI, XFXWIDTH, HEIGHT_TABLE)];
    [self.view addSubview:self.bigScrollView];
    self.bigScrollView.contentSize = CGSizeMake(XFXWIDTH * 5, HEIGHT_TABLE);
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.bounces = NO;
    self.bigScrollView.delegate = self;
    
    self.sliderView = [[UIView alloc] initWithFrame:CGRectMake(self.headLine.frame.origin.x, self.headLine.frame.origin.y + self.headLine.frame.size.height, self.headLine.frame.size.width, 2 * XFXSHEI)];
    self.sliderView.backgroundColor = [UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1];
    [self.view addSubview:self.sliderView];
    
    self.navigationItem.title = @"新闻";
    
    [self getButtonShow];
    [self createButtons];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

// 页面
- (void)getButtonShow{
    
    // 头条页面
    XFXNewsViewController *myTableVC = [[XFXNewsViewController alloc] init];
    [self addChildViewController:myTableVC];
    [self.bigScrollView addSubview:myTableVC.view];
    
    // 娱乐页面
    XFXRecreationViewController *listVC = [[XFXRecreationViewController alloc] init];
    [self addChildViewController:listVC];
    listVC.view.frame = CGRectMake(XFXWIDTH * 1, 0, XFXWIDTH, XFXHEIGHT);
    [self.bigScrollView addSubview:listVC.view];
    
    
    // 体育页面
    XFXSportViewController *authorVC = [[XFXSportViewController alloc] init];
    [self addChildViewController:authorVC];
    authorVC.view.frame = CGRectMake(XFXWIDTH * 2, 0, XFXWIDTH, XFXHEIGHT);
    [self.bigScrollView addSubview:authorVC.view];
    
    // 科技页面
    XFXScienceViewController *scienceVC = [[XFXScienceViewController alloc] init];
    [self addChildViewController:scienceVC];
    scienceVC.view.frame = CGRectMake(XFXWIDTH * 3, 0, XFXWIDTH, XFXHEIGHT);
    [self.bigScrollView addSubview:scienceVC.view];
    
    // 时尚页面
    XFXFashionViewController *fashionVC = [[XFXFashionViewController alloc] init];
    [self addChildViewController:fashionVC];
    fashionVC.view.frame = CGRectMake(XFXWIDTH * 4, 0, XFXWIDTH, XFXHEIGHT);
    [self.bigScrollView addSubview:fashionVC.view];
    
}

#pragma mark - 创建按钮
- (void)createButtons{
    
    // 头条按钮
    self.headLine = [UIButton buttonWithType:UIButtonTypeCustom];
    self.headLine.frame = CGRectMake(0, 0, XFXWIDTH / 5, 40 * XFXSHEI);
    [self.headLine setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
    [self.headLine setTitle:@"头条" forState:UIControlStateNormal];
    self.headLine.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.headLine];
    [self.headLine addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 娱乐按钮
    self.recreation = [UIButton buttonWithType:UIButtonTypeCustom];
    self.recreation.frame = CGRectMake(self.headLine.frame.origin.x + self.headLine.frame.size.width * 1, 0, XFXWIDTH / 5, self.headLine.frame.size.height);
    [self.recreation setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
    [self.recreation setTitle:@"娱乐" forState:UIControlStateNormal];
    self.recreation.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.recreation];
    [self.recreation addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 体育按钮
    self.sport = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sport.frame = CGRectMake(self.headLine.frame.origin.x + self.headLine.frame.size.width * 2, 0, XFXWIDTH / 5, self.headLine.frame.size.height);
    [self.sport setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
    [self.sport setTitle:@"体育" forState:UIControlStateNormal];
    self.sport.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.sport];
    [self.sport addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 科技按钮
    self.science = [UIButton buttonWithType:UIButtonTypeCustom];
    self.science.frame = CGRectMake(self.headLine.frame.origin.x + self.headLine.frame.size.width * 3, 0, XFXWIDTH / 5, self.headLine.frame.size.height);
    [self.science setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
    [self.science setTitle:@"科技" forState:UIControlStateNormal];
    self.science.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.science];
    [self.science addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 时尚按钮
    self.fashion = [UIButton buttonWithType:UIButtonTypeCustom];
    self.fashion.frame = CGRectMake(self.headLine.frame.origin.x + self.headLine.frame.size.width * 4, 0, XFXWIDTH / 5, self.headLine.frame.size.height);
    [self.fashion setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
    [self.fashion setTitle:@"时尚" forState:UIControlStateNormal];
    self.fashion.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.fashion];
    [self.fashion addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark 按钮点击方法
- (void)buttonAction:(UIButton *)btn{
    
    if (btn == self.headLine) {
        [self.bigScrollView setContentOffset:CGPointMake(0, 0 ) animated:YES];
    }else if (btn == self.recreation){
        [self.bigScrollView setContentOffset:CGPointMake(XFXWIDTH * 1, 0) animated:YES];
    }else if (btn == self.sport){
        [self.bigScrollView setContentOffset:CGPointMake(XFXWIDTH * 2, 0) animated:YES];
    }else if (btn == self.science){
        [self.bigScrollView setContentOffset:CGPointMake(XFXWIDTH * 3, 0) animated:YES];
    }else if (btn == self.fashion){
        [self.bigScrollView setContentOffset:CGPointMake(XFXWIDTH * 4, 0) animated:YES];
    }
}

#pragma mark - scrollView协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.sliderView.frame = CGRectMake(self.bigScrollView.contentOffset.x / 5, self.headLine.frame.origin.y + self.headLine.frame.size.height, self.headLine.frame.size.width, 2 * XFXSHEI);
    NSInteger num = self.bigScrollView.contentOffset.x / XFXWIDTH;
    switch (num) {
        case 0:
            [self.headLine setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
            [self.recreation setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.sport setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.science setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.fashion setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            break;
        case 1:
            [self.headLine setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.recreation setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
            [self.sport setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.science setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.fashion setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            break;
        case 2:
            [self.headLine setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.recreation setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.sport setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
            [self.science setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.fashion setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            break;
        case 3:
            [self.headLine setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.recreation setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.sport setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.science setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
            [self.fashion setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            break;
        case 4:
            [self.headLine setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.recreation setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.sport setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.science setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.fashion setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
            break;
        default:
            break;
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
